import { isPlainObject } from './util'

function normalizeHeaderName(header: any, normalizeName: string): void {
  header &&
    Object.keys(header).forEach(name => {
      if (name !== normalizeName && name.toUpperCase() === normalizeName.toUpperCase()) {
        header[normalizeName] = header[name]

        delete header[name]
      }
    })
}

export function processHeader(headers: any, data: any): void {
  normalizeHeaderName(headers, 'Content-Type')

  if (isPlainObject(data)) {
    if (headers && !headers['Content-Type']) {
      headers['Content-Type'] = 'application/json; chartset=utf-8'
    }
  }

  return headers
}

export function parseHeaders(headers: string): any {
  let parsed = Object.create(null)

  if (!headers) {
    return
  }

  headers.split('\r\n').forEach(line => {
    let [key, value] = line.split(':')
    key = key.trim().toLowerCase()

    if (!key) {
      return
    }

    if (value) {
      value = value.trim()
    }

    parsed[key] = value
  })

  return parsed
}
